<template>
	<view v-show="shareshow"  class="share">
		<view class="share-title u-f-ajc">
			分享到
		</view>
			<!-- 滑块 -->
			<scroll-view scroll-x class="scroll">
			<!-- 	<block v-for="(item ,index) in providerList" :key="index">
					<view @tap="share(item)" class="share-list"  hoverClass="haverclass">
							<u-icon :name="item.classname" :color="item.color" size="100"></u-icon>
							<view class="">	微信</view>
					</view>
				</block> -->
							<!-- 待完善 -->
				<view class="share-list"  hoverClass="haverclass">
						<u-icon name="weixin-circle-fill" color="#2AD19B" size="100"></u-icon>
						<view class="">	微信</view>
				</view>
				<view class="share-list"  hoverClass="haverclass">
						<u-icon name="weibo-circle-fill" color="  #EE5E5E" size="100"></u-icon>
						<view class="">	微博</view>
				</view>
				<view class="share-list"  hoverClass="haverclass">
						<u-icon name="weixin-fill" color="#514D4C" size="100"></u-icon>
						<view class="">	朋友圈</view>
				</view>
				<view class="share-list"  hoverClass="haverclass">
						<u-icon name="qq-circle-fill" color="#5280CE" size="100"></u-icon>
						<view class="">	QQ</view>
				</view>
				<view class="share-list"  hoverClass="haverclass">
						<u-icon name="more-dot-fill" color="#514D4C" size="100"></u-icon>
						<view class="">	其他</view>
				</view>
			</scroll-view>
			
			
		<view  hoverClass="haverclass" @tap="shareshow = false" class="share-bottom u-f-ajc">
			取消
		</view>	
	</view>
</template>

<script>
	export default{
		data() {
			return {
				
				shareText: 'uni-app可以同时发布成原生App、小程序、H5，邀请你一起体验！',
				href:"https://uniapp.dcloud.io",
				image: '',
				shareType:1,
				providerList: []
			}
		},
		props:{
			shareshow:Boolean
		},
		mounted() {
			console.log("在初始化分享")
			uni.getProvider({
				service: 'share',
				success: (e) => {
					
					let data = []
					for (let i = 0; i < e.provider.length; i++) {
						switch (e.provider[i]) {
							case 'weixin':
								data.push({
									name: '分享到微信好友',
									id: 'weixin',
									sort:0
								})
								data.push({
									name: '分享到微信朋友圈',
									id: 'weixin',
									type:'WXSenceTimeline',
									sort:1
								})
								break;
							case 'sinaweibo':
								data.push({
									name: '分享到新浪微博',
									id: 'sinaweibo',
									sort:2
								})
								break;
							case 'qq':
								data.push({
									name: '分享到QQ',
									id: 'qq',
									sort:3
								})
								break;
							default:
								break;
						}
					}
					this.providerList = data.sort((x,y) => {
						return x.sort - y.sort
					});
					console.log('success', this.providerList );
				},
				fail: (e) => {
					console.log('获取分享通道失败', e);
					uni.showModal({
						content:'获取分享通道失败',
						showCancel:false
					})
				}
			});
		},
		methods: {
			async share(e) {
				console.log('分享通道:'+ e.id +'； 分享类型:' + this.shareType);
				
				if(!this.shareText && (this.shareType === 1 || this.shareType === 0)){
					uni.showModal({
						content:'分享内容不能为空',
						showCancel:false
					})
					return;
				}
				
				if(!this.image && (this.shareType === 2 || this.shareType === 0)){
					uni.showModal({
						content:'分享图片不能为空',
						showCancel:false
					})
					return;
				}
				
				let shareOPtions = {
					provider: e.id,
					scene: e.type && e.type === 'WXSenceTimeline' ? 'WXSenceTimeline' : 'WXSceneSession', //WXSceneSession”分享到聊天界面，“WXSenceTimeline”分享到朋友圈，“WXSceneFavorite”分享到微信收藏     
					type: this.shareType,
					success: (e) => {
						console.log('success', e);
						uni.showModal({
							content: '已分享',
							showCancel:false
						})
					},
					fail: (e) => {
						console.log('fail', e)
						uni.showModal({
							content: e.errMsg,
							showCancel:false
						})
					},
					complete:function(){
						console.log('分享操作结束!')
					}
				}
				
				switch (this.shareType){
					case 0:
						shareOPtions.summary = this.shareText;
						shareOPtions.imageUrl = this.image;
						shareOPtions.title = '欢迎体验uniapp';
						shareOPtions.href = 'https://uniapp.dcloud.io';
						break;
					case 1:
						shareOPtions.summary = this.shareText;
						break;
					case 2:
						shareOPtions.imageUrl = this.image;
						break;
					case 5:
						shareOPtions.imageUrl = this.image ? this.image : 'https://img-cdn-qiniu.dcloud.net.cn/uniapp/app/share-logo@3.png'
						shareOPtions.title = '欢迎体验uniapp';
						shareOPtions.miniProgram = {
							id:'gh_33446d7f7a26',
							path:'/pages/tabBar/component/component',
							webUrl:'https://uniapp.dcloud.io',
							type:0
						};
						break;
					default:
						break;
				}
				
				if(shareOPtions.type === 0 && plus.os.name === 'iOS'){//如果是图文分享，且是ios平台，则压缩图片 
					shareOPtions.imageUrl = await this.compress();
				}
				if(shareOPtions.type === 1 && shareOPtions.provider === 'qq'){//如果是分享文字到qq，则必须加上href和title
					shareOPtions.href = 'https://uniapp.dcloud.io';
					shareOPtions.title = '欢迎体验uniapp';
				}
				uni.share(shareOPtions);
			},
			radioChange(e){
				console.log('type:' + e.detail.value);
				this.shareType = parseInt(e.detail.value);
			},
			chooseImage() {
				uni.chooseImage({
					count: 1,
					sourceType: ['album', 'camera'],
					sizeType: ['compressed', 'original'],
					success: (res) => {
						this.image = res.tempFilePaths[0];
					},
					fail: (err) => {
						// #ifdef MP
						uni.getSetting({
							success: (res) => {
								let authStatus = res.authSetting['scope.album'] && res.authSetting['scope.camera'];
								if (!authStatus) {
									uni.showModal({
										title: '授权失败',
										content: 'Hello uni-app需要从您的相机或相册获取图片，请在设置界面打开相关权限',
										success: (res) => {
											if (res.confirm) {
												uni.openSetting()
											}
										}
									})
								}
							}
						})
						// #endif
					}
				})
			},
			compress(){//压缩图片 图文分享要求分享图片大小不能超过20Kb
				console.log('开始压缩');
				let img = this.image;
				return new Promise((res) => {
					var localPath = plus.io.convertAbsoluteFileSystem(img.replace('file://', ''));
					console.log('after' + localPath);
					// 压缩size
					plus.io.resolveLocalFileSystemURL(localPath, (entry) => {
						entry.file((file) => {// 可通过entry对象操作图片 
							console.log('getFile:' + JSON.stringify(file));
							if(file.size > 20480) {// 压缩后size 大于20Kb
								plus.zip.compressImage({
									src: img,
									dst: img.replace('.jpg', '2222.jpg').replace('.JPG', '2222.JPG'),
									width: '10%',
									height: '10%',
									quality: 1,
									overwrite: true
								}, (event) => {
									console.log('success zip****' + event.size);
									let newImg = img.replace('.jpg', '2222.jpg').replace('.JPG', '2222.JPG');
									res(newImg);
								}, function(error) {
									uni.showModal({
										content:'分享图片太大,需要请重新选择图片!',
										showCancel:false
									})
								});
							}
						});
					}, (e) => {
						console.log('Resolve file URL failed: ' + e.message);
						uni.showModal({
							content:'分享图片太大,需要请重新选择图片!',
							showCancel:false
						})
					});
				})
			}
		}
		
	}
</script>

<style scoped>
	.share{
		z-index: 100;
		background-color: #FFFFFF;
		position: fixed;
		bottom: 0;
		width: 100%;
	}
	.scroll{
		display: flex;
		width: 100%;
		height: 200upx;
		white-space: nowrap;
		align-items: center;
		
	}
	.haverclass{
		background-color: #EEEEEE;
	}
	.share-title,.share-bottom{
		font-size: 35upx;
		padding: 10upx;
		border-top: #EEEEEE 1UPX solid;
	}
	.share-list{
		height: 100%;
		width: 25%;
		display: inline-flex;
		align-items: center;
		justify-content: space-around;
		flex-direction: column;
	}
	
</style>
